//
//  PrivateLetterViewController.m
//  MJApp
//
//  Created by scjh on 15/5/12.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "PrivateLetterViewController.h"
#import "PrivateLetterCollectionViewCell.h"
#import "ReplyLetterViewController.h"

@interface PrivateLetterViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,HomeCellDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *listArray;

@end

@implementation PrivateLetterViewController

- (NSMutableArray *)listArray {
    
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UI];
    [self obationData];
}

- (void)UI {
    
    self.title = @"私信";
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(IOS_IPHONE_WINDOW_WIDE - 20, 200);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
    flowLayout.minimumLineSpacing = 20;
    self.collectionView.collectionViewLayout = flowLayout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = Color_Back_Black;
    [self.collectionView registerNib:[UINib nibWithNibName:@"PrivateLetterCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PrivateLetterCollectionViewCell"];
}

- (void)obationData {
    
    NSDictionary *dic = @{@"user_id":[userInfoTool share].ID};
    [WLHttpRequestAfter userGetMyLetterWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        
        if ([resultDictionary[@"data"] isKindOfClass:[NSArray class]]) {
            
            [self.listArray removeAllObjects];
            [self.listArray addObjectsFromArray:resultDictionary[@"data"]];
            [self.collectionView reloadData];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PrivateLetterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PrivateLetterCollectionViewCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.infoDic = self.listArray[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)homeCell:(id)cell withInfo:(id)info {
    
    NSDictionary *dic = (NSDictionary *)info;
    NSString *stateStr = info[@"state"];
    if ([stateStr isEqual:@"delete"]) {
        
        //delete
        [self deleteLetter:dic];
    }else {
        
        //reply
        [self replyLetter:dic];
    }
}

- (void)deleteLetter:(NSDictionary *)dic {
    
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    dic1[@"id"] = dic[@"id"];
    [WLHttpRequestAfter userDeleteLetterWithParam:dic1 usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [self obationData];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)replyLetter:(NSDictionary *)dic {
    
    ReplyLetterViewController *replyVC = [[ReplyLetterViewController alloc] initWithDic:dic];
    replyVC.title = dic[@"nick_name"];
    [self.navigationController pushViewController:replyVC animated:YES];
}

@end
